package com.muyu.rabbitmq.producter;

import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @ClassName Producer
 * @Description 描述
 * @Author ShiYu.Cui
 * @Date 2024/4/8 16:32
 */
@RestController
@Slf4j
public class Producer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/direct/wx/transactional")
    @Transactional(rollbackFor = Exception.class)
    public String sendDirectMessageTransactional(){
        rabbitTemplate.convertAndSend("directExchange","direct.transactional",message -> {
            message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
            return message;
        });
        //模拟抛出异常
        int i=1/0;
        log.info("开启事务消息机制");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
            return "ok";
    }
}
